/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wxrobot.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.util.GeneratePoster;
import com.jeesite.modules.wxrobot.dao.CustomerDao;
import com.jeesite.modules.wxrobot.entity.Customer;
import com.jeesite.modules.wxrobot.entity.TaskActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 活动客户信息Service
 * @author laozhang
 * @version 2018-07-31
 */
@Service
@Transactional(readOnly=true)
public class CustomerService extends CrudService<CustomerDao, Customer> {

	@Autowired
	private TaskActivityService taskActivityService;

	/**
	 * 获取单条数据
	 * @param customer
	 * @return
	 */
	@Override
	public Customer get(Customer customer) {
		return super.get(customer);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param customer
	 * @return
	 */
	@Override
	public Page<Customer> findPage(Page<Customer> page, Customer customer) {
		return super.findPage(page, customer);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param customer
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Customer customer) {
		super.save(customer);
	}
	
	/**
	 * 更新状态
	 * @param customer
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Customer customer) {
		super.updateStatus(customer);
	}

	/**
	 * 更新机器人微信用户名
	 * @param customer
	 */
    @Transactional(readOnly=false)
	public void updateRobotWeixin(Customer customer) {
		this.dao.updateRobotWeixin(customer);
	}
	
	/**
	 * 删除数据
	 * @param customer
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Customer customer) {
		super.delete(customer);
	}

	public String getMaxSelfCodeByActivityId(String activityId){
		return this.dao.getMaxSelfCodeByActivityId(activityId);
	}

	public Map<String,String> generateExclusivePoster(String activityId,String selfCode,String realPath){
		GeneratePoster generatePoster = new GeneratePoster();

		TaskActivity activity = taskActivityService.get(activityId);
		String originalPoster =  activity.getPoster();
		String originalPosterFileSuffix = originalPoster.substring(originalPoster.lastIndexOf("."),originalPoster.length());
		String originalPosterFileName = originalPoster.substring(originalPoster.lastIndexOf("/")+1,originalPoster.lastIndexOf("."));
		String originalPosterFolder = realPath+originalPoster.substring(0,originalPoster.lastIndexOf("/"));

		//创建用于存放客户专属海报的文件夹
		File file = new File(originalPosterFolder+"/"+originalPosterFileName);
		if(!file.exists()){
			file.mkdir();
		}
		String exclusivePosterPath = originalPosterFolder+"/"+originalPosterFileName+"/"+originalPosterFileName+"_"+selfCode+originalPosterFileSuffix;
		generatePoster.graphicsGeneration( realPath+originalPoster,selfCode+"",exclusivePosterPath);

		//专属海报图片上下文路径
		String exclusivePosterContextPath = originalPoster.substring(0,originalPoster.lastIndexOf("/"))+"/"+originalPosterFileName+"/"+originalPosterFileName+"_"+selfCode+originalPosterFileSuffix;
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("exclusivePosterPath",exclusivePosterPath);
		resultMap.put("exclusivePosterContextPath",exclusivePosterContextPath);
		resultMap.put("code",selfCode+"");
		return resultMap;
	}

	@Transactional(readOnly=false)
	public int updateDiscountStatus(Customer customer){
		return this.dao.updateDiscountStatus(customer);
	}
}